<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\ImportCouponExcel;
use App\Admin\Extension\CouponCodeSmallCard;
use App\Admin\Repositories\CouponType;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class CouponTypeController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CouponType(), function (Grid $grid) {

            $grid->enableDialogCreate();
            $grid->showEditButton(false);
            $grid->showQuickEditButton();
            $grid->disableFilterButton();

            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('description');
            $grid->column('pic')->image();
            $grid->column('total');
            $grid->column('used');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();


//            $grid->actions(new ImportCouponExcel());

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->prepend('<button>按钮</button>');

                $actions->append(new ImportCouponExcel());
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    public function show($id, Content $content)
    {

        return $content->header('Post')
            ->description('详情')
            ->body(function (Row $row) use ($id) {

                $row->column(12, Show::make($id, new CouponType(), function (Show $show) {
                    $show->field('title')->width(4);
                    $show->field('description')->width(4);
                }));

                $row->column(12, '<br/>');
                $row->column(12, CouponCodeSmallCard::make(['type_id'=>   $id]));

            });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CouponType(), function (Form $form) {
            $form->display('id');
            $form->text('title')->rules('required');
            $form->textarea('description');
            $form->image('pic')->rules('required|image');
            $form->number('total')->default(1)->min(1);
//            $form->text('used');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
